package com.controller;

import com.bean.Emp;
import com.bean.Msg;
import com.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class EmpController {
    @Autowired
    EmpService empService;

    @RequestMapping(value = "/addEmp",method = RequestMethod.POST)
    public String addEmp(Emp emp, @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){
        empService.addEmp(emp);
        return "redirect:/list?pageNumber="+pageNum; //跳转到list页面最后一页
    }
    @RequestMapping(value = "updateEmp/{pageNumber}",method = RequestMethod.PUT)
    public String updateEmp(Emp emp,@PathVariable(value = "pageNumber") int pageNum){
        empService.updateEmp(emp);
        System.out.println(pageNum);
        return "redirect:/list?pageNumber="+pageNum; //跳转到list页面当前页
    }
    @RequestMapping(value = "deleteEmp/{ids}/{pageNumber}",method = RequestMethod.DELETE)
    public String deleteEmp(@PathVariable(value = "ids") List<Integer> ids, @PathVariable(value = "pageNumber") int pageNum){
        if(ids.size()==1){
            empService.deleteEmp(ids.get(0));
        }else{
            empService.deleteManyEmp(ids);
        }
        return "forward:/listWithAjax?pageNumber="+pageNum;
    }
}
